package com.txsj.scan.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.adapter.ChangeSelectedGoodsAdapter;
import com.txsj.scan.bean.GoodsInfo;

import java.util.List;

/**
 * @Created by WangChuangJun on 2018/11/21
 * @Describe: 选择商品列表页进行商品筛选
 */
public class ChangeSelectedGoodsListDialog extends Dialog implements View.OnClickListener {
    private Activity activity;
    private TextView cancel_selected, tv_select_all_count;
    private LinearLayout lay_confirm;
    private RelativeLayout root;

    private RecyclerView recyclerView;
    private ChangeSelectedGoodsAdapter adapter;

    private Listener listener;

    /**
     * 此list在获取到数据后，会有如下操作
     * adapter
     */
    private List<GoodsInfo> list;//要操作的数据类型


    public ChangeSelectedGoodsListDialog(Activity activity, List<GoodsInfo> list) {
        super(activity, R.style.ThemeCustomDialog);
        super.setContentView(R.layout.dialog_change_selected_goods_list);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        this.activity = activity;
        this.list = list;
        initView();
        initData();
    }

    /**
     * 初始化数据
     */
    private void initData() {
        adapter = new ChangeSelectedGoodsAdapter(activity);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(activity);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        adapter.setList(list);
        adapter.setListener(new ChangeSelectedGoodsAdapter.OnItemCheckListener() {
            @Override
            public void onCheckChange(int position) {
                /**
                 * 根据dubug发现，adapter中的list和当前list是同一个list
                 * 因此只需要改变adapter中的选中状态即可
                 * 在点确定的时候进行筛选
                 */
                adapter.updateRightData(position);//设置选中状态
                updateAllCount();
            }
        });
        updateAllCount();
    }

    /**
     * 更新选中状态
     */
    private void updateAllCount() {
        tv_select_all_count.setText(adapter.getSelectedList().size() + "");
    }


    /**
     * 初始化布局文件
     */
    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        root = (RelativeLayout) findViewById(R.id.root);
        cancel_selected = (TextView) findViewById(R.id.cancel_selected);
        tv_select_all_count = (TextView) findViewById(R.id.tv_select_all_count);
        lay_confirm = (LinearLayout) findViewById(R.id.lay_confirm);
        cancel_selected.setOnClickListener(this);
        lay_confirm.setOnClickListener(this);
        root.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel_selected://取消全选
                adapter.clearSelect();
                updateAllCount();
                break;
            case R.id.lay_confirm://确认提交
                if (null != listener) listener.onConfirm(adapter.getSelectedList());
                dismiss();
                break;
            case R.id.root://点击其他地方
                dismiss();
                break;
        }
    }


    /**
     * 将要操作的数据传进来
     *
     * @param list
     */
    private void setData(List<GoodsInfo> list) {
        this.list = list;
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public interface Listener {
        void onConfirm(List<GoodsInfo> list);
    }
}
